﻿//===================================================================
//
// Description: Sparqube Picture Column Lite for SharePoint 2010
// Web: http://www.sparqube.com/
//
//-------------------------------------------------------------------
//
// Copyright (C) 2012 ArtfulBits.CZ (development[at]artfulbits.cz)
// Web: http://www.artfulbits.cz/
//
//-------------------------------------------------------------------
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE.
//===================================================================

using System.Security.Permissions;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.WebControls;

namespace Sparqube.SharePoint.PictureColumnLite
{
	public sealed class PictureLiteField :
		SPFieldMultiLineText
	{
		#region Constants

		internal const string Property_StorageFolderUrl = "StorageFolderUrl";
		internal const string Property_ThumbnailWidth = "ThumbWidth";
		internal const string Property_ThumbnailHeight = "ThumbHeight";

		internal const int Default_ThumbnailWidth = 100;
		internal const int Default_ThumbnailHeight = 0;

		#endregion //Constants

		#region Fields

		private FieldAdapter m_adapter = null;

		#endregion //Fields

		#region Properties

		internal FieldAdapter Adapter
		{
			get
			{
				if (null == m_adapter)
				{
					m_adapter = new FieldAdapter(this);
					InitializeAdapter(m_adapter);
				}
				return m_adapter;
			}
		}

		#endregion //Properties

		#region Constructors

		public PictureLiteField(SPFieldCollection fields, string fieldName) :
			base(fields, fieldName)
		{
			this.RichText = true;
			this.SetRichTextMode(SPRichTextMode.FullHtml);

			this.Adapter.Initialize();
		}

		public PictureLiteField(SPFieldCollection fields, string typeName, string displayName) :
			base(fields, typeName, displayName)
		{
			this.RichText = true;
			this.SetRichTextMode(SPRichTextMode.FullHtml);

			this.Adapter.Initialize();
		}

		#endregion //Constructors

		#region Properties

		public string StorageFolderUrl
		{
			get
			{
				return (string)this.Adapter.GetProperty(Property_StorageFolderUrl);
			}
			set
			{
				this.Adapter.SetProperty(Property_StorageFolderUrl, value, false);
			}
		}

		public int ThumbnailWidth
		{
			get
			{
				return (int)this.Adapter.GetProperty(Property_ThumbnailWidth, Default_ThumbnailWidth);
			}
			set
			{
				this.Adapter.SetProperty(Property_ThumbnailWidth, value, false);
			}
		}

		public int ThumbnailHeight
		{
			get
			{
				return (int)this.Adapter.GetProperty(Property_ThumbnailHeight, Default_ThumbnailHeight);
			}
			set
			{
				this.Adapter.SetProperty(Property_ThumbnailHeight, value, false);
			}
		}

		#endregion //Properties

		#region Overrides

		public override object GetFieldValue(string value)
		{
			PictureLiteValue val = null;
			if (!string.IsNullOrEmpty(value))
			{
				val = new PictureLiteValue(value);
			}
			return val;
		}

		public override BaseFieldControl FieldRenderingControl
		{
			[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
			get
			{
				BaseFieldControl fieldControl = new PictureLiteFieldControl();
				fieldControl.FieldName = this.InternalName;

				return fieldControl;
			}
		}

		public override void Update()
		{
			this.Adapter.OnStartUpdate();
			base.Update();
			this.Adapter.OnEndUpdate();
		}

		public override void OnUpdated()
		{
			base.OnUpdated();
			this.Adapter.OnUpdated();
		}

		public override void OnAdded(SPAddFieldOptions op)
		{
			base.OnAdded(op);
			this.Adapter.OnAdded();
		}

		#endregion //Overrides

		#region Implementation

		private void InitializeAdapter(FieldAdapter adapter)
		{
			adapter.AddFieldProperty(Property_StorageFolderUrl, false, null);
			adapter.AddFieldProperty(Property_ThumbnailWidth, true, Default_ThumbnailWidth);
			adapter.AddFieldProperty(Property_ThumbnailHeight, true, Default_ThumbnailHeight);
		}

		#endregion //Implementation

	}// PictureLiteField
}
